package cn.zjh.pet.service.impl;

import cn.zjh.basic.exception.BusinessException;
import cn.zjh.basic.service.IBaiduAiAuditService;
import cn.zjh.basic.service.impl.BaseServiceImpl;
import cn.zjh.basic.util.LoginContext;
import cn.zjh.org.domain.Employee;
import cn.zjh.org.mapper.EmployeeMapper;
import cn.zjh.pet.domain.Pet;
import cn.zjh.pet.domain.PetDetail;
import cn.zjh.pet.domain.PetOnlineAuditLog;
import cn.zjh.pet.mapper.PetDetailMapper;
import cn.zjh.pet.mapper.PetMapper;
import cn.zjh.pet.mapper.PetOnlineAuditLogMapper;
import cn.zjh.pet.service.IPetService;
import cn.zjh.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IBaiduAiAuditService iBaiduAiAuditService;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;


    /**
     * 添加
     * @param pet
     */
    @Transactional
    @Override
    public void save(Pet pet) {
        super.save(pet);
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail!=null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.save(petDetail);
        }
    }

    @Override
    public void upDate(Pet pet) {
        super.upDate(pet);
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail!=null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.updatePetDetailByPetId(petDetail);
        }
    }

    @Transactional
    @Override
    public void del(Long id) {
        super.del(id);
        petDetailMapper.delPetDetailByPetId(id);
    }

    /**
     * 上架审核
     * @param ids
     */
    @Override
    public void onsale(Long[] ids, HttpServletRequest request) {
        //获取登录人信息
        Logininfo logininfo = LoginContext.getLoginUser(request);
        //获取审核人
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());

        for (Long id : ids) {
            Pet pet = petMapper.loadById(id);
            //上架之前对图文本和图片做审核
            Boolean textAudit = iBaiduAiAuditService.textAudit(pet.getName());

            Boolean imageAudit = iBaiduAiAuditService.imageAudit(pet.getResources());
            //审核通过之后,修改状态和资料
            if (textAudit && imageAudit){
                pet.setOnsaletime(new Date());
                pet.setState(1);
                petMapper.upDate(pet);

                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setAuditId(employee.getId());
                log.setAuditTime(new Date());
                log.setState(1);
                log.setPetId(pet.getId());
                log.setNote("审核通过！");
                petOnlineAuditLogMapper.save(log);

            }else {
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setAuditId(employee.getId());
                log.setAuditTime(new Date());
                log.setState(-1);//1审核通过  0待审核  -1驳回
                log.setPetId(pet.getId());
                log.setNote("审核失败，含有违规内容！");
                petOnlineAuditLogMapper.save(log);
                throw new BusinessException("审核失败，内容有非法数据");
            }

        }

    }

    /**
     * 下架
     * @param ids
     */
    @Override
    public void offsale(Long[] ids) {

        Date offsaletime = new Date();
        petMapper.offSale(offsaletime,ids);
    }
}
